package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.google.gson.annotations.JsonAdapter;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * reggie-com.itheima.filter
 * Administrator
 * 2023-03-06
 * 19:55
 */
@Slf4j
@WebFilter("/*")
public class LoginFilter implements Filter {
    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        HttpSession session = req.getSession();
        Employee attribute = (Employee) session.getAttribute("emp");
        String []a={
                //"/login","/backend/api"
        //        ,"/backend/images"
        //        ,"/backend/js"
        //        ,"/backend/page"
        //        ,"/backend/plugins"
        //        ,"/backend/styles"
        "/employee/login",
                "/employee/logout",
                "/backend",
                "/front",
                "/front/page",
                "/common",
                "/user/sendMsg",
                "/user/login"
    };
        String URL = req.getRequestURL().toString();
        //2、判断本次请求是否需要处理
        System.out.println(URL);
        for (String s : a) {
            if (URL.contains(s)){
                filterChain.doFilter(servletRequest,servletResponse);
                return;
            }
        }

        if (attribute!=null){
            Long id = attribute.getId();
            //在doFilter方法中, 判定用户是否登录, 如果用户登录, 在放行之前, 获取HttpSession中的登录用户信息,
            //        调用BaseContext的setCurrentId方法将当前登录用户ID存入ThreadLocal。
            BaseContext.setCurrentId(id);
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        //else {
        //    R<Object> notlogin = R.error("NOTLOGIN");
        //    //R notlogin = new R(0, "NOTLOGIN", null, null);
        //    System.out.println(notlogin);
        //    String jsonString = JSON.toJSONString(notlogin);
        //    res.getWriter().write(jsonString);
        //}

        //判断登录状态，如果已登录，则直接放行
        if(req.getSession().getAttribute("user") != null){
            log.info("用户已登录，用户id为：{}",req.getSession().getAttribute("user"));

            Long userId = (Long) req.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);

            filterChain.doFilter(req,res);
            return;
        }

        res.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    @Override
    public void destroy() {

    }
}
